const path = require('path')
const fs =require('fs')
class Source{

    constructor(){
        this.sourcePath = path.resolve(__dirname,'./') 
        this.nameList = []
        this.sourceList=[]
    }

    // 初始化加载资源库
    init(){
        return new Promise((resolve,reject)=>{
            const {sourcePath} =this

            fs.readdir(this.sourcePath, (err, files) => {
                if (!err) {
                    files
                        .filter(item => item !== 'index.js')
                        .forEach((file, index) => {
                            const obj = require(path.resolve(sourcePath, `./${file}`))
                            this.nameList.push(obj.name)
                            this.sourceList.push(obj)
                        })
                    console.trace(`添加数据源:${this.nameList.join(',')}`)
                    resolve(this)
                } else {
                    reject(`读取${sourcePath}   文件夹失败`)
                }
            })
        })
    }
}


module.exports = Source
